<?php
include_once("match.php");
if(class_exists('SQL_Helper') != true)
{
   include_once("sql_helper.php");
}
class MatchRepository
{
	function createMatch($roundSchaduleId, $player1Id, $player2Id, $matchNumber){
		SQL_Helper::executeNonQuery("INSERT INTO `match` (`matchID`, `round_scheduleID`, `player1ID`, `player2ID`, `match_number`) VALUES(null,?,?,?,?)",$roundSchaduleId, $player1Id, $player2Id, $matchNumber);
	}
	
	function readMatch($matchId){
		return SQL_Helper::executeQuerySingle("Select * from `match` where `matchId` = ?", $matchId);
	}
	
	function updateMatch($matchId, $roundSchaduleId, $player1Id, $player2Id, $match_number){
		SQL_Helper::executeNonQuery("Update `match` set `round_scheduleID`=?, `player1ID`=?, `player2ID`=?, `match_number`=? where `matchId` = ?",$roundSchaduleId, $player1Id, $player2Id, $match_number, $matchId);
	}
	
	function deleteMatch($matchId){
		SQL_Helper::executeNonQuery("delete from `match` where `matchId` = ?", $matchId);
	}
	
	function map($row){
		$class= "Match";
		return SQL_Helper::sql_map($row,$class);
	}
	
	function maps($rows){
		$class= "Match";
		return SQL_Helper::sql_maps($rows,$class);
	}


	function getMatchByRoundScheduleID($round_scheduleID){
		return SQL_Helper::executeQuery("Select `matchID`,`round_scheduleID`, `player1ID`, `player2ID`,`x1`.`match_number`, `player1_letter`, `player2_letter` from `match` as `x1` inner join `player_sequence`as `x2` on `x1`.`match_number`=`x2`.`match_number`  where `round_scheduleID` = ? order by `x1`.`match_number`", $round_scheduleID);
		
	}
	
	function getMatchByRoundScheduleIDandPlayerID($round_scheduleID,$playerID){
		return SQL_Helper::executeQuery("Select * from `match` where `round_scheduleID` = ? and (`player1ID`=? or `player2ID`=?)order by `match_number` ", $round_scheduleID,$playerID,$playerID);
	}
	
	function updateMatchPlayer1($roundSchaduleId, $player1IdOld, $player1IdNew){
		SQL_Helper::executeNonQuery("Update `match` set `player1ID`=? where `round_scheduleID`=? and `player1ID`=? ", $player1IdNew, $roundSchaduleId,  $player1IdOld);
	}
	
	function updateMatchPlayer2($roundSchaduleId, $player2IdOld, $player2IdNew){
		SQL_Helper::executeNonQuery("Update `match` set `player2ID`=? where `round_scheduleID`=? and `player2ID`=? ", $player2IdNew, $roundSchaduleId,  $player2IdOld);
	}
	
	function getMatchByRounds($firstRoundID, $lastRoundID){
		return SQL_Helper::executeQuery("SELECT * FROM `match` WHERE `round_scheduleID` in (Select `round_scheduleID` from `round_schedule` where `roundID`>=? and  `roundID`<=?)", $firstRoundID, $lastRoundID);
	}

function getMatchByRoundsDivisions($firstRoundID, $lastRoundID, $divisionID){
		return SQL_Helper::executeQuery("SELECT * FROM `match` WHERE `round_scheduleID` in (Select `round_scheduleID` from `round_schedule` where `roundID`>=? and  `roundID`<=? AND  (`type` NOT LIKE  'final' OR  `type` IS NULL) and `team1` in (select `teamID` from `teams` where `divisionID`=?))", $firstRoundID, $lastRoundID, $divisionID);
		
	}
	
	function updateMatchNumber($match_number, $roundSchaduleId, $player1Id, $player2Id){
		SQL_Helper::executeNonQuery("Update `match` set `match_number`=? where `round_scheduleID`=? and `player1ID`=? and `player2ID`=?",$match_number, $roundSchaduleId, $player1Id, $player2Id);
	}


function getplayerT1byRoundScheduleID($roundSchaduleId){
		 return SQL_Helper::executeQuery("SELECT `playerID`, `fname`,`sname` FROM `players` WHERE `playerID` in (
select distinct `player1ID` from `match` WHERE `round_scheduleID`=?)",$roundSchaduleId);
	}
	function getplayerT2byRoundScheduleID($roundSchaduleId){
		 return SQL_Helper::executeQuery("SELECT `playerID`, `fname`,`sname` FROM `players` WHERE `playerID` in (
select distinct `player2ID` from `match` WHERE `round_scheduleID`=?)",$roundSchaduleId);
	}
}

?>